<?php
namespace App\Controller;

use App\Model\UserModel;
use lib\View;

class UserController
{
    public function register(){
        unset($_SESSION);
		return new View('register');
// 销毁session中的错误数据包 （错误数据包是一次性的）
    }

    public function do_register(){
    	if (IS_POST) {
    		if (!empty($_POST['password']) && !empty($_POST['confirmpass']) && !empty($_POST['userName']) && !empty($_POST['email']) ) 
    		{
    		if ($_POST['password'] === $_POST['confirmpass'] ) {
    		$password = password_hash($_POST['password'],PASSWORD_DEFAULT);
    		$user = new UserModel();
            $users = $user->get();
           
            foreach ($users as $key => $value) {
                if($_POST['userName']==$value['username']){
                    error('用户已被注册');
                }
            }
    		$data['email'] =$_POST['email'];
  			$data['username'] =$_POST['userName'];
  			$data['password'] =$password;
    		$user->save($data);
    		success("注册成功",'login');
    		}
    		else {
    			$_SESSION['error_bag'] ='两次输入密码不一致';
    		}
    		}
    		else{
    			$_SESSION['error_bag'] ='用户信息不能为空';
    		}

    		$errorBag = $_SESSION['error_bag'];
    		unset($_SESSION['error_bag']);
    		error('注册失败,'.$errorBag);
    	}
    }
    public function login(){
        unset($_SESSION);
        return new View('login');
    }

    public function do_login(){
            
    	if (IS_POST && !empty($_POST['userName']) && !empty($_POST['password'])) 
    	{
    	$isLogin = 0;
        $userName = $_POST['userName'];
        $inputPassword = $_POST['password'];
    	$user = new UserModel();
    	$data =$user->get(['username' => $_POST['userName']]);
        

		if (!empty($data) ) 
		{	
            if($data[0]['is_online'] == 1){
                $d['id'] = $data[0]['id'];
                $d['is_online'] = $isLogin;
                $user->save($d);
            error('该用户已经登录','login');
        }
			if (password_verify($inputPassword, $data[0]['password'])) 
			{   
				$isLogin = 1;
				$_SESSION['islogin'] = $isLogin;
                $d['id'] = $data[0]['id'];
                $d['is_online'] = $isLogin;
                $user->save($d);
				$_SESSION['user'] = $data[0]['username'];
                $_SESSION['user_id'] = $data[0]['id'];
				success('登录成攻','index');
			} 
        }
        else
            {    
                $_SESSION['error_bag'] ='用户名或密码错误';
                $errorBag =$_SESSION['error_bag'];
             }	

	}
		else{	
				$_SESSION['error_bag'] ='信息不能为空';
				$errorBag =$_SESSION['error_bag'];
			
		}
            unset($_SESSION['error_bag']);
            error('登录失败,'.$errorBag);
    

  }

  public function out_login(){
        $user =$_SESSION['user'];
        $user_id=$_SESSION['user_id'];
        $get_user = new UserModel();
        $data =$get_user->get(['username' => $user]);
        $d['id']=$_SESSION['user_id'];
        $d['is_online'] = 0;
        $d['last_login'] = date('Y-m-d H:i:s',time());
        $get_user->save($d);
         session_destroy();
        success('退出登录','login');
  }
}
